
package com.yuanma.android.boan;

//import android.app.Activity;
import android.support.v7.app.*;

import android.support.v4.view.GestureDetectorCompat;
import android.view.GestureDetector.SimpleOnGestureListener;

import android.content.Intent;
import android.os.Bundle;
import android.view.*;
import android.widget.TextView;
import android.widget.ScrollView;
import java.util.Vector;
import android.util.Log;


public class MessageActivity extends ActionBarActivity {

    protected int msg_no;

    private GestureDetectorCompat mDetector; 
    private class MyGestureListener extends SimpleOnGestureListener {   
	private static final int SWIPE_MIN_DISTANCE = 120;   
	private static final int SWIPE_MAX_OFF_PATH = 250;   
	private static final int SWIPE_THRESHOLD_VELOCITY = 200; 
	/*	viewFlipper = (ViewFlipper)findViewById(R.id.flipper);   
        slideLeftIn = AnimationUtils.loadAnimation(this, R.anim.slide_left_in);   
        slideLeftOut = AnimationUtils.loadAnimation(this, R.anim.slide_left_out);   
        slideRightIn = AnimationUtils.loadAnimation(this, R.anim.slide_right_in);   
        slideRightOut = AnimationUtils.loadAnimation(this, R.anim.slide_right_out);
	*/

	@Override  
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
				float distanceY) {
	    if(distanceX>50) return false;

	    ScrollView vscr=(ScrollView)findViewById(R.id.content);
	    vscr.scrollBy(0,(int)distanceY);
	    return true;
	}

	@Override  
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {   
	    if(Math.abs(e1.getY() - e2.getY()) > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
		ScrollView vscr=(ScrollView)findViewById(R.id.content);
		vscr.fling(-(int)velocityY);
		return true;
		
	    }
	    if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {   
		//viewFlipper.setInAnimation(slideLeftIn);   
		//viewFlipper.setOutAnimation(slideLeftOut);   
		//viewFlipper.showNext(); 
		changeMessage(msg_no+1);  
		return true;
	    } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {   
		/*	viewFlipper.setInAnimation(slideRightIn);   
			viewFlipper.setOutAnimation(slideRightOut);   
			viewFlipper.showPrevious();
		*/
		changeMessage(msg_no-1);   
		return true;
	    }
	    return false;
	}
    }
    
    @Override  
    public boolean onTouchEvent(MotionEvent event) {   

        this.mDetector.onTouchEvent(event);
        return super.onTouchEvent(event);
    }  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mDetector = new GestureDetectorCompat(this, new MyGestureListener());
    }

    protected  Vector<Message> getMessageFromService()
    {
                    if (Constants.LOGD) {
                        
                        Log.e("boanMsg", this.getClass().getName());
			
                    }
	
	return (this.getClass().getName().compareTo("com.yuanma.android.boan.MessageReadActivity")==0)
	    ?JsonPullService.msgsToRead:JsonPullService.msgsToReview;
	
    }
    
    public void changeMessage(int sn)
    {
	msg_no=sn;
	Vector<Message> vMessage=getMessageFromService();
	
	int mSize=vMessage.size();
	
	if(msg_no<0) msg_no=0;
	if(msg_no>mSize-1) msg_no=mSize-1;
	
        Message msg;
	if( JsonPullService.loginName.length()==0) msg = new Message(0,true," ",getString(R.string.not_login_message));
	else if(mSize==0) msg = new Message(0,true," ",getString(R.string.no_message));
	else msg=vMessage.get(msg_no);

	((TextView) findViewById(R.id.message_title)).setText(msg.title);
	((TextView) findViewById(R.id.message_content)).setText(msg.content);
	
	supportInvalidateOptionsMenu();
	
	//invalidateOptionsMenu();

    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.activity_message, menu);

	Vector<Message> vMessage=getMessageFromService();

	 menu.findItem(R.id.action_previous).setEnabled(msg_no > 0); 
	 menu.findItem(R.id.action_next).setEnabled(msg_no < (vMessage.size() - 1));

        // Add either a "next" or "finish" button to the action bar, depending on which page
        // is currently selected.
	/* MenuItem item = menu.add(Menu.NONE, R.id.action_next, Menu.NONE,
                (msg_no == titles.length - 1)
                        ? R.string.action_finish
                        : R.string.action_next);
        item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
	*/
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
	case android.R.id.home:
	    // Navigate "up" the demo structure to the launchpad activity.
	    // See http://developer.android.com/design/patterns/navigation.html for more.
	    return super.onOptionsItemSelected(item);
	case R.id.action_previous:
	    // Go to the previous step in the wizard. If there is no previous step,
	    // setCurrentItem will do nothing.
	    changeMessage(msg_no-1);
	    return true;

	case R.id.action_next:
	    // Advance to the next step in the wizard. If there is no next step, setCurrentItem
	    // will do nothing.
	    changeMessage(msg_no+1 );
	    return true;
        }
	
        return super.onOptionsItemSelected(item);
    }


}
